package com.imooc.product.controller;

/***
 *  商品
 */

import com.imooc.product.VO.ProductInfoVO;
import com.imooc.product.VO.ProductVO;
import com.imooc.product.VO.ResultVO;
import com.imooc.product.common.DecreaseStockInput;
import com.imooc.product.common.ProductInfoOutput;
import com.imooc.product.dataobject.ProductCategory;
import com.imooc.product.dataobject.ProductInfo;
import com.imooc.product.service.CategoryService;
import com.imooc.product.service.ProductService;
import com.imooc.product.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
/*@RequestMapping("/com/imooc/product")*/
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService  productService ;

    @Autowired
    private CategoryService categoryService ;

    /**
     * 1.查询所有在架的商品
     * 2.获取类目type列表
     * 3.查询类目
     * 4.构造数据
     */
    @GetMapping("/list")
    public ResultVO<ProductVO> list(){

        //1.查询所有在架的商品
        List<ProductInfo>  productInfoList = productService.findUpAll() ;

        //2.获取类目type列表
        List<Integer>   categoryTypeList = productInfoList.stream().map(ProductInfo::getCategoryType).collect(Collectors.toList());

        //3.查询类目
        List<ProductCategory>  productCategoryList =  categoryService.findByCategoryTypeIn(categoryTypeList);

        //4.构造数据
        List<ProductVO> productVOList = new ArrayList<ProductVO>( );
        for(ProductCategory productCategory : productCategoryList) {
            ProductVO productVO = new ProductVO() ;

            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo  :productInfoList){
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO  = new ProductInfoVO() ;
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }

            productVO.setProductInfoVOList(productInfoVOList);

            productVOList.add(productVO);
        }

     return ResultVOUtil.success(productVOList);
    }


    /**
     * 给订单提供的列表
     * @param productIdList
     * @return
     */
    @PostMapping("/listForOrder")
    public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList){
        System.out.println(productIdList);
        return  productService.findList(productIdList);

    }

    /**
     * 扣库存
     * @param decreaseStockInputList
     */
    @PostMapping("/decreaseStock")
    public void  decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList){

        productService.decreaseStock(decreaseStockInputList);
    }

}
